import axiosIns from '../../utils/axios';
import update, { updateChain } from 'immutability-helper-x';
import { collectList, delCollections } from '../../utils/mock';
/**
|--------------------------------------------------
|  Constants
|--------------------------------------------------
*/
const COL_LOADING = 'COL_LOADING';
const GETLISTED = 'GETLISTED';
const DELETING = 'DELETING';
const DELETED = 'DELETED';
const FILTERLIST = 'FILTERLIST';
/**
|--------------------------------------------------
| Actions
|--------------------------------------------------
*/
export function getList(params) {
  return async (dispatch, getState) => {
    const { loading } = getState().collect;
    if (loading) return;
    dispatch(fetching());
    const res = await axiosIns('/getCollections');
    dispatch(getlistSuccess(res.data));
    dispatch(filter({ type: 'all' }));
  };
}
export function delCollect(arr) {
  return async (dispatch, getState) => {
    const { deleting } = getState().collect;
    if (deleting) return;
    // dispatch(deleting());
    const res = axiosIns('/delCollect');
    const { list, datasource } = getState().collect;
    let list_copy = [].concat(list);
    let datasource_copy = [].concat(datasource);
    for (let item of arr) {
      for (let [indexl, l] of list_copy.entries()) {
        if (item == l.id) {
          list_copy.splice(indexl, 1);
          break;
        }
      }
      for (let [indexs, s] of datasource_copy.entries()) {
        if (item == s.id) {
          datasource_copy.splice(indexs, 1);
          break;
        }
      }
    }
    dispatch(delSuccess({ list: list_copy, datasource: datasource_copy }));
  };
}

export function filter(params) {
  return async (dispatch, getState) => {
    const { datasource } = getState().collect;
    let list = [].concat(datasource);
    const { type } = params;
    if (type != 'all') {
      list = list.filter(item => item.type == type);
    }
    dispatch({
      type: FILTERLIST,
      list
    });
  };
}
function fetching() {
  return {
    type: COL_LOADING
  };
}
function deleting() {
  return {
    type: DELETING
  };
}
function getlistSuccess(data) {
  return {
    type: GETLISTED,
    list: data.list
  };
}

function delSuccess(data) {
  return {
    type: DELETED,
    list: data.list,
    datasource: data.datasource
  };
}

/**
|--------------------------------------------------
| Action Handlers
|--------------------------------------------------
*/
const ACTION_HANDLERS = {
  [COL_LOADING]: state => {
    return { ...state, loading: true };
  },
  [DELETING]: state => {
    return { ...state, deleting: true };
  },
  [GETLISTED]: (state, action) => {
    return { ...state, loading: false, datasource: action.list };
  },
  [DELETED]: (state, action) => {
    return {
      ...state,
      loading: false,
      datasource: action.datasource,
      list: action.list
    };
  },
  [FILTERLIST]: (state, action) => {
    return { ...state, list: action.list };
  }
};
/**
|--------------------------------------------------
| Reducer
|--------------------------------------------------
*/
const initialState = {
  datasource: null,
  list: null,
  loading: false,
  deleting: false
};
export default function(state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type];
  return handler ? handler(state, action) : state;
}
